<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="{ type: 'checkbox' }">
      <template #headerTop>
        <TableSelect :selectRows="getSelectRows()" @clear="clearSelectedRowKeys()" />
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleAdd"><Icon icon="ant-design:appstore-add-outlined"></Icon> 创建</a-button>
        <a-button type="primary" @click="handleEdit(getSelectRows()[0])" :disabled="getSelectRows().length !== 1"><Icon icon="ant-design:edit-outlined"></Icon> 编辑</a-button>
        <a-button type="primary" @click="handleDelete(null, true)" :disabled="getSelectRows().length < 1"><Icon icon="ant-design:delete-outlined"></Icon> 删除</a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              label: '修改',
              onClick: handleEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              label: '删除',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: handleDelete.bind(null, record, false),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <DataSourceModal @register="registerModal" @success="reload" />
  </div>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';
  import { BasicTable, TableAction, useTable } from '/@/components/Table';
  import { getBasicColumns, getFormConfig } from './config';
  import { useModal } from '/@/components/Modal';
  import DataSourceModal from './modal.vue';
  import { Icon } from '/@/components/Icon';
  import * as _API from './api';
  import CRUD from '/@/crud/core/init';
  import TableSelect from '/@/components/Custom/TableSelect/index.vue';
  export default defineComponent({
    name: 'DataSource',
    components: { Icon, BasicTable, DataSourceModal, TableAction, TableSelect },
    setup() {
      const crud = CRUD({ title: '数据源', crudMethod: { ..._API } }); // 初始化crud
      const [registerModal, { openModal }] = useModal();
      const [registerTable, { reload, getSelectRowKeys, getSelectRows, clearSelectedRowKeys }] = useTable({
        api: _API.getAPI,
        afterFetch(info) {
          return info;
        },
        columns: getBasicColumns(),
        formConfig: getFormConfig(),
        useSearchForm: true,
        showTableSetting: true,
        size: 'small',
        bordered: true,
        showIndexColumn: true,
        canResize: false,
        clickToRowSelect: false,
        rowKey: 'id',
        actionColumn: {
          width: 120,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right',
        },
      });

      function handleAdd() {
        openModal(true, {
          isUpdate: false,
        });
      }

      function handleEdit(record: Recordable) {
        openModal(true, {
          record,
          isUpdate: true,
        });
      }

      /**
       * @vuese
       * @Description: 删除事件
       * @arg {*} record
       * @arg {*} useConfirm
       * @since: 2022-03-30
       */
      async function handleDelete(record: Recordable, useConfirm: Boolean) {
        crud
          .toDel({
            params: {
              ids: (record && [record.id]) ?? getSelectRowKeys(),
            },
            useConfirm,
            refresh: false,
          })
          .then((res) => {
            reload();
          });
      }
      return {
        registerTable,
        registerModal,
        getSelectRows,
        handleAdd,
        handleEdit,
        handleDelete,
        clearSelectedRowKeys,
        reload,
      };
    },
  });
</script>
